package com.paipai.test;

import com.alibaba.fastjson.JSON;
import com.paipai.config.RabbitConfig;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Component
public class BookHandler {
	/**
	 * 监听的是私信队列
	 * @param message
	 * @param channel
	 * @param headers
	 * @throws UnsupportedEncodingException
	 */
	@RabbitListener(queues = { RabbitConfig.REGISTER_QUEUE_NAME })
	public void listenerDelayQueue(Message message, Channel channel, @Headers Map<String, Object> headers) throws UnsupportedEncodingException {
		System.out.println("延时队列接受到消息");

		// 获取消息的主体内容
		String msgBody = new String(message.getBody(), "UTF-8");
		@SuppressWarnings("unchecked")
		HashMap<String, Object> map = JSON.parseObject(msgBody, HashMap.class);
		System.out.println(map);
		try {
			// TODO 通知 MQ 消息已被成功消费,可以ACK了
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (IOException e) {
			// TODO 如果报错了,那么我们可以进行容错处理,比如转移当前消息进入其它队列
		}
	}
}
